package com.zeng.niceservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.nicecore.constant.IgnoreConstant;
import com.zeng.nicecore.util.BeanUtil;
import com.zeng.nicecore.util.IgnoreStringUtil;
import com.zeng.nicedao.mapper.SystemUserMapper;
import com.zeng.nicepojo.entity.SystemUser;
import com.zeng.nicepojo.vo.SystemUserVo;
import com.zeng.niceservice.service.SystemUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SystemUserServiceImpl implements SystemUserService {

    @Autowired
    SystemUserMapper systemUserMapper;

    @Override
    public SystemUserVo addOne(SystemUserVo systemUserVo) {
    SystemUser systemUser = SystemUser.builder().build();
    BeanUtils.copyProperties(systemUserVo, systemUser, IgnoreStringUtil.ignoreExclude());
    int insert = systemUserMapper.insert(systemUser);
    return BeanUtil.beanToVo(systemUser, systemUserVo);
    }

    @Override
    public SystemUserVo getOneByCode(String code) {

    SystemUser systemUser = this.systemUserMapper.selectById(code);
    SystemUserVo systemUserVo = BeanUtil.beanToVo(systemUser, new SystemUserVo());

    return systemUserVo;
    }

    @Override
    public Integer update(SystemUserVo systemUserVo) {
    SystemUser systemUser = BeanUtil.VoToBean(systemUserVo,
    new SystemUser(),
    IgnoreStringUtil.ignoreExclude(IgnoreConstant.code.getValue()));
    return this.systemUserMapper.updateById(systemUser);
    }

    @Override
    public Integer delete(String code) {
    return this.systemUserMapper.deleteById(code);
    }

    @Override
    public Integer batchDelete(List<String> codes) {

    return this.systemUserMapper.deleteBatchIds(codes);
    }

    @Override
    public List<SystemUserVo> findPage(Page page, SystemUserVo systemUserVo) {
        SystemUser systemUser = BeanUtil.VoToBean(systemUserVo, new SystemUser());
        this.systemUserMapper.selectPage(page, this.getWrapper(systemUserVo));
        List orders = page.getRecords();
        List<SystemUserVo> systemUserVos = new ArrayList<>();
        orders.forEach(e->{
            systemUserVos.add(BeanUtil.beanToVo(e, new SystemUserVo()));
        });
        page.setRecords(null);
        return systemUserVos;
        }


    @Override
    public Integer count(SystemUserVo systemUserVo) {
        Integer integer = this.systemUserMapper.selectCount(this.getWrapper(systemUserVo));
        return integer;
    }

    public AbstractWrapper getWrapper(SystemUserVo systemUserVo) {
        QueryWrapper<SystemUser> wrapper = new QueryWrapper<>();
        if (systemUserVo != null) {

                if (StringUtils.isNotEmpty(systemUserVo.getRealName())) {
                    wrapper.like("realName", systemUserVo.getRealName());
                }
                if (StringUtils.isNotEmpty(systemUserVo.getMobile())) {
                    wrapper.like("mobile", systemUserVo.getMobile());
                }

        }
        return wrapper;
    }
}
